import { ref} from 'vue';
import { datasourceList,deleteDatasource,batchDeleteDatasource } from '@/api/datasource';
import type {DsListQueryParams} from '@/interface/datasource'
import {ElMessage} from 'element-plus';

// 数据源查询table列表部分
export function useDatasourceQuery() {
  // 数据源列表
  const sourceList = ref<any[]>([]);
  // 数据源总数
  const sourceTotal = ref(0);
  // 请求参数
  const queryParameters = ref<DsListQueryParams>({
    name: '',
    typeCode: '0101',
    page: 1,
    rows: 15,
    orderBy: 'createTime desc'
  });
  // 获取数据源列表
  const getSourceTable = async () => {
    let res: any = await datasourceList(queryParameters.value);
    sourceList.value = res.data.records;
    sourceTotal.value = res.data.total;
  };
  // 列表页码变更
  const pageChange = (pageNum: number) => {
    queryParameters.value.page = pageNum;
    getSourceTable();
  };
  // 重新查询数据
  const queryChange = (params: any) => {
    queryParameters.value.name = params.datasourceName;
    queryParameters.value.orderBy = `${params.selectField} ${params.sortMethod}`;
    queryParameters.value.page = 1;
    getSourceTable();
  };
  /*
  * @function deleteSource 删除数据源方法
  * @param row table当前行数据
  * */
  const deleteSource = async(type:string,data: any) => {
    let deleteRes = null
    if (type === 'single') {
      deleteRes = await deleteDatasource(data.idString)
    }
    if (type ==='multiple') {
      deleteRes = await batchDeleteDatasource({id:data})
    }
    if (!deleteRes){
      return
    }
    if (deleteRes.status === 200){
      ElMessage({
        message: deleteRes.msg,
        type:'success'
      })
      getSourceTable()
    } else {
      ElMessage({
        message: deleteRes.msg,
        type: 'error'
      })
    }
  }
  return { sourceList, sourceTotal, queryParameters, getSourceTable, pageChange, queryChange,deleteSource };
}

// 数据源新增编辑部分
export function useSourceAddAndEdit() {
  // 数据库数据源基础信息
  const _databaseInfo = {
    type: 'postgreSQL',
    typeCode: '0101',
    name: '',
    ip: '',
    port: 5432,
    database: '',
    user: '',
    password: '',
    remark: ''
  };
  // 文件夹/共享文件夹数据源基础信息
  const _fileSourceInfo = {
    type: 'shapeFolder',
    typeCode: '0101'
  };
  // 是否打开数据源新增弹窗
  const addFrameShow = ref(false);
  // 数据源类型
  const dataSourceTypes = ref([
    {
      label: '数据库',
      value: '数据库'
    },
    {
      label: '文件夹',
      value: '文件夹'
    },
    {
      label: '共享文件夹',
      value: '共享文件夹'
    }
  ]);
  // 数据类型
  const dataTypes = ref([
    {
      label: 'Oracle',
      value: 'oracle',
      type: '数据库'
    },
    {
      label: 'PostgreSQL',
      value: 'postgreSQL',
      type: '数据库'
    },
    {
      label: 'shapeFolder',
      value: 'shapeFolder',
      type: '文件夹,共享文件夹'
    },
    {
      label: 'fgdb',
      value: 'fgdb',
      type: '文件夹,共享文件夹'
    },
    {
      label: 'mdb',
      value: 'mdb',
      type: '文件夹,共享文件夹'
    }
  ]);
  // 数据源添加参数
  const sourceAddParams = ref(_databaseInfo);
  //数据源类型
  const sourceType = ref('数据库');
  // 打开数据源新增弹窗
  const openAddFrame = () => {
    addFrameShow.value = true;
  };
  return { addFrameShow, dataSourceTypes, dataTypes, sourceAddParams, sourceType, openAddFrame };
}
